package com.karson.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.karson.common.exception.AsmrException;
import com.karson.common.model.*;
import com.karson.common.request.PageParamRequest;
import com.karson.common.response.CommonResult;
import com.karson.service.service.AlbumInfoService;
import com.karson.service.service.CirclesInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/circles")
@Tag(name = "社群管理接口")
public class CirclesController {
    @Autowired
    private CirclesInfoService circlesInfoService;
    @Autowired
    private AlbumInfoService albumInfoService;

    @Operation(summary = "分页列表")
    @GetMapping("/list")
    public CommonResult<IPage<CirclesInfo>> getList(@ModelAttribute @ParameterObject PageParamRequest pageParamRequest, @RequestParam(required = false) String keyword) {
        IPage<CirclesInfo> circlesInfoIPage = new Page<>(pageParamRequest.getCurrent(), pageParamRequest.getSize());
        IPage<CirclesInfo> tagInfo =  circlesInfoService.pageList(circlesInfoIPage,keyword);
        return CommonResult.success(tagInfo);
    }
    @Operation(summary = "添加标签")
    @PostMapping("/add")
    public CommonResult<Void> addCircles(@RequestBody CirclesInfo request) {
        CirclesInfo circlesInfo = circlesInfoService.getOne(new QueryWrapper<CirclesInfo>().eq("circle_name", request.getCircleName()));
        if (circlesInfo != null) {
            return CommonResult.failed("重复添加社群");
        }
        request.setWorkCount(0);
        boolean save = circlesInfoService.save(request);
        return save ? CommonResult.success() : CommonResult.failed();
    }
    @Operation(summary = "更改社群信息")
    @PutMapping("/update")
    public CommonResult<String> update (@RequestBody CirclesInfo request) {
        if (null == request.getId() || request.getId() <= 0) {
            throw new AsmrException("id 参数不合法");
        }
        LambdaQueryWrapper<CirclesInfo> circlesQueryWrapper = new LambdaQueryWrapper<>();
        circlesQueryWrapper.eq(CirclesInfo::getCircleName, request.getCircleName());
        circlesQueryWrapper.ne(CirclesInfo::getId, request.getId());
        CirclesInfo circlesInfo = circlesInfoService.getOne(circlesQueryWrapper);
        if (circlesInfo != null) {
            return CommonResult.failed("重复添加社群");
        }
        boolean update = circlesInfoService.updateById(request);
        return update ? CommonResult.success() : CommonResult.failed();
    }
    @Operation(summary = "删除社群信息")
    @DeleteMapping("/delete/{id}")
    public CommonResult<String> delete(@PathVariable("id") Integer id) {
        LambdaQueryWrapper<AlbumInfo> albumInfoQueryWrapper = new LambdaQueryWrapper<>();
        albumInfoQueryWrapper.eq(AlbumInfo::getCircleId, id);
        List<AlbumInfo> albumInfoList = albumInfoService.list(albumInfoQueryWrapper);
        if (!albumInfoList.isEmpty()){
            return CommonResult.failed("别删求你");
        }
        boolean flag = circlesInfoService.removeById(id);
        return flag ? CommonResult.success("删除成功") : CommonResult.failed("删除失败");
    }
    @Operation(summary = "根据id获取社群信息")
    @GetMapping("/getCircleById/{id}")
    public CommonResult<CirclesInfo> getCircleById(@PathVariable("id") Integer id) {
        CirclesInfo circlesInfo = circlesInfoService.getById(id);
        return CommonResult.success(circlesInfo);
    }
}
